import time

from tools.base_page import BasePage


class CertiFication(BasePage):

    def certi_daquan(self):
        certi_locator = ('id', 'com.znb.zxx:id/allCertificateImg')
        first_certi_detail = ('id', 'com.znb.zxx:id/item_info_title')

        self.find(certi_locator).click()
        self.elements(first_certi_detail)[0].click()

    def certi_detail(self):
        baokao_condition = ('id', 'com.znb.zxx:id/indicatorTitleTv')
        time.sleep(2)
        for num in range(3):
            self.elements(baokao_condition)[num].click()
            time.sleep(2)
            self.go_swipe(0.5, 0.8, 0.5, 0.2)
            time.sleep(2)
        self.page(self.index)
        return True

    def certi_type(self):
        certi_left_type = ('id', 'com.znb.zxx:id/item_sort_main_title')
        certi_right_type = ('id', 'com.znb.zxx:id/item_sort_second_title')
        certi_locator = ('id', 'com.znb.zxx:id/allCertificateImg')

        self.find(certi_locator).click()
        for num in range(2, 8):
            self.elements(certi_left_type)[num].click()
            for j in range(1, 4):
                time.sleep(2)
                self.elements(certi_right_type)[j].click()
                time.sleep(2)
        self.page(self.index)
        return True

    def certi_search(self, text_value):
        search_locator = ('id', 'com.znb.zxx:id/searchIV')
        seach_res = ('id', 'com.znb.zxx:id/item_title')
        certi_locator = ('id', 'com.znb.zxx:id/allCertificateImg')
        seach_input = ('id', 'com.znb.zxx:id/search_edit_text')

        self.find(certi_locator).click()
        self.find(search_locator).click()
        self.find(seach_input).send_keys(text_value)
        self.find(seach_res).click()

    def exist_certi_detail(self):
        certi_title = ('id', 'com.znb.zxx:id/certificateTitle')
        if self.is_element(certi_title) == True:
            self.page(self.index)
            return True
        else:
            self.page(self.index)
            return False

    def cert_news(self):
        news_locator = ('id', 'com.znb.zxx:id/allNewsImg')
        first_news = ('id', 'com.znb.zxx:id/comm_item_news_title')
        self.find(news_locator).click()
        self.elements(first_news)[1].click()
        self.page(self.index)

    def cert_course(self):
        cat_more_open_course = (
            'xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多公开课")]')
        courese_list_title = ('xpath',
                              '//*[@class="androidx.recyclerview.widget.RecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 课程列表中的课程标题
        time.sleep(2)
        self.swipeUp()
        self.find(cat_more_open_course).click()
        self.find(courese_list_title).click()
        self.page(self.index)

    def add_cert_skill(self):
        skill_locator = ('id', 'com.znb.zxx:id/skillAddTitle')
        exist_sikll_list = ('id', 'item_skill_title')
        setting = ('id', 'skillUpChange')
        add_locator = ('text', '添加')

        time.sleep(2)
        self.go_swipe(0.5, 0.7, 0.5, 0.2)
        time.sleep(2)
        try:
            if self.find(skill_locator).text == '技能提升':
                self.single_add_skill()
        except:
            if len(self.elements(exist_sikll_list)) == 5:
                self.skill_equal_to_five()
            else:
                self.find(setting).click()
                self.find_element(add_locator).click()
            return self.skill_if_or_not_add()

    def skill_if_or_not_add(self):
        '''
        循环遍历每个职业尝试添加，如果职业添加过就返回找下一个。如果未添加就添加
        :return:
        '''
        off_hand_study = ('id', 'com.znb.zxx:id/leanBtn')
        text_name = "立即学习"
        for num in range(1, 6):
            right_add_type = (
                'xpath',
                '//*[@resource-id="com.znb.zxx:id/lastSortChildItemRv"]/android.view.ViewGroup[{}]'.format(num))
            if self.find_locator_and_back(locator=right_add_type, location=off_hand_study, text_name=text_name,
                                          want_location=off_hand_study) is True:
                self.page(self.index)
                return True

    def single_add_skill(self):
        '''
        当有技能时，添加一条
        :return:
        '''
        skill_locator = ('id', 'com.znb.zxx:id/skillAddTitle')
        first_skill = ('id', 'com.znb.zxx:id/item_info_title')
        skill_study_type = ('id', 'com.znb.zxx:id/indicatorTitleTv')
        off_hand_study = ('id', 'com.znb.zxx:id/leanBtn')
        self.find(skill_locator).click()
        self.elements(first_skill)[1].click()
        for i in range(1, 3):
            self.elements(skill_study_type)[i].click()
            time.sleep(1)
        self.find(off_hand_study).click()
        self.page(self.index)
        return True

    def skill_equal_to_five(self):
        '''
        技能有五条时全部删除，添加一个
        :return:
        '''
        del_locator = ('id', 'com.znb.zxx:id/item_skill_del_iv')
        confiirm = ('id', 'actionConfirm')
        setting = ('id', 'skillUpChange')
        add_skill = ('id', 'com.znb.zxx:id/skillAddEnterIv')

        time.sleep(2)
        self.find(setting).click()
        ele = len(self.elements(del_locator))
        for num in range(ele):
            self.elements(del_locator)[0].click()
            time.sleep(1)
            self.find(confiirm).click()
            if num == ele - 1:
                self.find(add_skill).click()
            else:
                self.find(setting).click()
